package tt.com.warehousemanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tt.com.common.model.YxCommonWarehouseStock;
import tt.com.common.model.YxCommonWarehouseStockRecord;
import tt.com.common.util.PageUtil;
import tt.com.warehousemanager.mapper.WarehouseStockMapper;
import tt.com.warehousemanager.service.WarehouseGoodsService;
import tt.com.warehousemanager.service.WarehouseStockService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author Czycka
 * @date 2022/8/23 15:58
 * @description 仓库库存实现类
 */
@Service
public class WarehouseStockServiceImpl extends ServiceImpl<WarehouseStockMapper, YxCommonWarehouseStock> implements WarehouseStockService {

    @Autowired
    WarehouseGoodsService goodsService;

    @Override
    public Integer inStock(YxCommonWarehouseStockRecord record, String userId) {
        // 创建条件构造器
        QueryWrapper<YxCommonWarehouseStock> wrapper = new QueryWrapper<>();
        // 传入物品id
        wrapper.eq("goods_id", record.getGoodsId());
        YxCommonWarehouseStock stockRecord = baseMapper.selectOne(wrapper);
        // 查询仓库内是否已存在该物品
        if (stockRecord == null) {
            // 若不存在该物品 则创建新的物品库存
            YxCommonWarehouseStock stock = new YxCommonWarehouseStock();
            // 设置仓库id
            stock.setWarehouseId(record.getWarehouseId());
            // 设置物品id
            stock.setGoodsId(record.getGoodsId());
            // 设置物品数量
            stock.setCount(record.getCount());
            // 设置创建时间
            stock.setCreateTime(new Date());
            // 设置创建操作人
            stock.setCreateUserId(userId);
            // 插入表中
            return baseMapper.insert(stock);
        } else {
            // 若存在该物品 则添加数量即可
            stockRecord.setCount(stockRecord.getCount().add(record.getCount()));
            return baseMapper.updateById(stockRecord);
        }
    }

    @Override
    public Integer inStockOfBatch(List<YxCommonWarehouseStockRecord> records, String userId) {
        Integer result = 0;
        for (YxCommonWarehouseStockRecord record :
                records) {
            // 创建条件构造器
            QueryWrapper<YxCommonWarehouseStock> wrapper = new QueryWrapper<>();
            // 传入物品id
            wrapper.eq("goods_id", record.getGoodsId());
            YxCommonWarehouseStock stockRecord = baseMapper.selectOne(wrapper);
            // 查询仓库内是否已存在该物品
            if (stockRecord == null) {
                // 若不存在该物品 则创建新的物品库存
                YxCommonWarehouseStock stock = new YxCommonWarehouseStock();
                // 设置仓库id
                stock.setWarehouseId(record.getWarehouseId());
                // 设置物品id
                stock.setGoodsId(record.getGoodsId());
                // 设置物品数量
                stock.setCount(record.getCount());
                // 设置创建时间
                stock.setCreateTime(new Date());
                // 设置创建操作人
                stock.setCreateUserId(userId);
                // 插入表中
                result = baseMapper.insert(stock);
            } else {
                // 若存在该物品 则添加数量即可
                stockRecord.setCount(stockRecord.getCount().add(record.getCount()));
                result = baseMapper.updateById(stockRecord);
            }
        }
        return result;
    }

    @Override
    public Integer outStock(YxCommonWarehouseStockRecord record, String userId) {
        // 创建条件构造器
        QueryWrapper<YxCommonWarehouseStock> wrapper = new QueryWrapper<>();
        // 传入物品id
        wrapper.eq("goods_id", record.getGoodsId());
        // 获取库存信息
        YxCommonWarehouseStock stockRecord = baseMapper.selectOne(wrapper);
        // 仓库内的库存数量
        BigDecimal stockCount = stockRecord.getCount();
        // 要出库的数量
        BigDecimal recordCount = record.getCount();
        // 将仓库数量减去要出库的数量 获得出库数量
        BigDecimal result = stockCount.subtract(recordCount);
        // 获取物品数量最低预警
        String organId = goodsService.getById(record.getGoodsId()).getOrganId();
        // 判断库存情况
        if (result.compareTo(BigDecimal.valueOf(0)) < 0) {
            // 仓库库存不足 无法出库
            return -2;
        } else if (result.compareTo(BigDecimal.valueOf(Long.parseLong(organId))) < 0) {
            // 仓库库存预警
            // 修改仓库剩余
            stockRecord.setCount(result);
            // 设置需要进货标识
            stockRecord.setBuyOutStock(1);
            baseMapper.updateById(stockRecord);
            return -1;
        } else {
            // 正常出库
            // 修改仓库剩余
            stockRecord.setCount(result);
            return baseMapper.updateById(stockRecord);
        }
    }

    @Override
    public Integer outStockOfBatch(List<YxCommonWarehouseStockRecord> records, String userId) {
        for (YxCommonWarehouseStockRecord record :
                records) {
            // 创建条件构造器
            QueryWrapper<YxCommonWarehouseStock> wrapper = new QueryWrapper<>();
            // 传入物品id
            wrapper.eq("goods_id", record.getGoodsId());
            // 获取库存信息
            YxCommonWarehouseStock stockRecord = baseMapper.selectOne(wrapper);
            // 仓库内的库存数量
            BigDecimal stockCount = stockRecord.getCount();
            // 要出库的数量
            BigDecimal recordCount = record.getCount();
            // 将仓库数量减去要出库的数量 获得出库数量
            BigDecimal result = stockCount.subtract(recordCount);
            // 获取物品数量最低预警
            String organId = goodsService.getById(record.getGoodsId()).getOrganId();
            // 判断库存情况
            if (result.compareTo(BigDecimal.valueOf(0)) < 0) {
                // 仓库库存不足 无法出库
                return -2;
            } else if (result.compareTo(BigDecimal.valueOf(Long.parseLong(organId))) < 0) {
                // 仓库库存预警
                // 修改仓库剩余
                stockRecord.setCount(result);
                // 设置需要进货标识
                stockRecord.setBuyOutStock(1);
                baseMapper.updateById(stockRecord);
                return -1;
            } else {
                // 正常出库
                // 修改仓库剩余
                stockRecord.setCount(result);
                return baseMapper.updateById(stockRecord);
            }
        }
        return -2;
    }

    @Override
    public PageUtil showByPage(PageUtil pageUtil) {
        Page<YxCommonWarehouseStock> page = new Page<>(pageUtil.getPageIndex(), pageUtil.getPageSize());
        Page<YxCommonWarehouseStock> stockPage = baseMapper.selectPage(page, null);
        pageUtil.setTotalSize((int) stockPage.getTotal());
        pageUtil.setData(stockPage);
        return pageUtil;
    }
}
